package com.jquery.service;

import java.sql.ResultSet;
import java.util.List;

import com.jquery.bean.Emp;
import com.jquery.bean.User;

import my.dao.Dao;
import my.dao.DaoCreater;
import my.dao.PreSQL;

public class EmpService {
	private Dao dao = DaoCreater.createDao();
	//查询员工
	public List<Emp> queryEmp() {
		
		try{
			//开启事务
			dao.beginTransaction();
			
			List<Emp> list = dao.queryBeanList(Emp.class,"select * from tbl_emp");
			
			//提交事务
			dao.commitTransaction();
			return list;
		}catch(Exception e){
			//事务回滚
			dao.rollbackTransaction();
			throw new RuntimeException();
		}finally{
			//dao.close();
		}
		
	}
	
	//根据删除某一员工
	public int deleteEmp(String empId) {
		try{
			//开启事务
			dao.beginTransaction();
			int i = dao.update("delete from tbl_emp where emp_id=?",empId );
			if(i>0){
				System.out.println("受影响的行数为:"+i);
				//提交事务
				dao.commitTransaction();
				return i;
				
			}
		}catch(Exception e){
			//事务回滚
			dao.rollbackTransaction();
			throw new RuntimeException();
		}finally{
			//dao.close();
		}
		return 0;
	}
	
	//根据删除某一员工
		public void deleteEmp(String[] empIds) {
			try{
				//开启事务
				dao.beginTransaction();
				PreSQL psql = dao.preSQL("delete from tbl_emp where emp_id=?");
				for(String empId :empIds){
					psql.update(empId);
				}
				dao.commitTransaction();
					
			}catch(Exception e){
				//事务回滚
				dao.rollbackTransaction();
				throw new RuntimeException();
			}finally{
				//dao.close();
			}
		}
	
	//修改员工信息
	public boolean updateEmpInfo(Emp emp){
		String sql="update tbl_emp set emp_name=?,emp_salary=?,emp_date=? where emp_id=?";
		Object[] obj={emp.getEmp_name(),emp.getEmp_salary(),emp.getEmp_date(),emp.getEmp_id()};
		try{
			dao.beginTransaction();
			int i = dao.update(sql, obj);
			if(i>0){
				System.out.println("修改成功!");
				dao.commitTransaction();
				return true;
			}
		}catch(Exception e){
			dao.rollbackTransaction();
			throw new RuntimeException();
		}finally{
			//dao.close();
		}
		
		return false;
	}
	
	//插入数据
	public boolean insertEmp(Emp emp){
		String sql = "insert into tbl_emp(emp_id,emp_name,emp_salary,emp_date) values(default,?,?,?)";
		Object[] obj = {emp.getEmp_name(),emp.getEmp_salary(),emp.getEmp_date()};
		try{
			dao.beginTransaction();
			int i = dao.update(sql, obj);
			if(i>0){
				dao.commitTransaction();
				return true;
			}
		}catch(Exception e){
			dao.rollbackTransaction();
			throw new RuntimeException();
		}finally{
			//dao.close();
		}
		return false;
	}

	public Emp queryEmpById(int empId) {
		String sql = "select * from tbl_emp where emp_id=?";
		try{
			dao.beginTransaction();
			List<Emp> emps = dao.queryBeanList(Emp.class, sql, empId);
			if(emps!=null){
				for(int i=0;i<emps.size();i++){
					dao.commitTransaction();
					return emps.get(0);
				}
			}
		}catch(Exception e){
			dao.rollbackTransaction();
			throw new RuntimeException();
		}finally{
			//dao.close();
		}
		
		return null;
	}
	
	//登录
	public User login(String username){
		try{
			dao.beginTransaction();
			List<User> users = dao.queryBeanList(User.class,"select * from tbl_user where user_name=?", username);
			if(users!=null){
				User user = users.get(0);
				if(user!=null){
					dao.commitTransaction();
					return user;
				}
			}
		
		}catch(Exception e){
			dao.rollbackTransaction();
			throw new RuntimeException();
		}finally{
			//dao.close();
		}
		
		return null;
	}
	
}
